package com.lry.basic.jvm.constantPool;

public class TestIntern {

//    public static void main(String[] args) {
//        String s1 = new String("1");
//        s1.intern();
//        String s2 = "1";
//        System.out.println(s1 == s2);
//
//
//        String s3 = new String("1") + new String("2");
//        s3.intern();
//        String s4 = "12";
//        System.out.println(s3 == s4);
//    }


//    public static void main(String[] args) {
//        String s1 = "hello";
//        String s2 = " world";
//        String s3 = s1+s2;
//
//        s3.intern();
//        String s4 = "hello world";
//        System.out.println(s4 == s3);
//    }

//        public static void main(String[] args) {
//            String str1 = new StringBuilder("计算机").append("软件").toString();
//            System.out.println(str1.intern()==str1);//输出true
//
//            String str2 = new StringBuilder("ja").append("va").toString();
//            System.out.println(str2.intern() == str2);//输出false
//        }

    public static void main(String[] args) {
        String str1 = "a";//str1指向常量池a
        String str2 = "b";//str2指向常量池b
        String str3 = "ab";//str3指向常量池ab
        String str4 = str1 + str2;//str4指向堆String obj,obj的char[] value指向常量池ab
        String str5 = new String("ab");//str5指向堆String obj, obj的char[] value指向常量池ab

        System.out.println(str5 == str3);//f
        System.out.println(str5.intern() == str3);//t
        System.out.println(str5.intern() == str4);//f

        String a = new String("ab");//a指向堆String obj, obj的char[] value指向常量池ab
        String b = new String("ab");//b指向堆String obj, obj的char[] value指向常量池ab
        String c = "ab";//c指向常量池ab
        String d = "a" + "b";//d指向常量池ab (编译期确定)
        String e = "b";//e指向常量池b
        String f = "a" + e;//f指向堆String obj,obj的char[] value指向常量池ab (StringBuilder实现)

        System.out.println(b.intern() == a);//f
        System.out.println(b.intern() == c);//t
        System.out.println(b.intern() == d);//t
        System.out.println(b.intern() == f);//f
        System.out.println(b.intern() == a.intern());//t

    }

}
